import { http } from './http';
/**
 * [MOCK-STORE-CREATION] Store Creation DataSource
 */

export interface WebsiteTemplate {
  id: string;
  name: string;
  category: 'ecommerce' | 'blog' | 'portfolio' | 'landing';
  layout: Record<string, any>;
  styles: Record<string, any>;
  components: string[];
  status: 'draft' | 'published';
}

export interface PageConfig {
  id: string;
  websiteId: string;
  name: string;
  path: string;
  components: PageComponent[];
  seoConfig: Record<string, any>;
}

export interface PageComponent {
  id: string;
  type: string;
  props: Record<string, any>;
  position: { x: number; y: number; w: number; h: number };
}

export interface BrandConfig {
  id: string;
  name: string;
  logo?: string;
  colors: Record<string, string>;
  fonts: Record<string, string>;
  story?: string;
  values: string[];
}

export interface MultilingualConfig {
  id: string;
  websiteId: string;
  defaultLanguage: string;
  supportedLanguages: string[];
  translations: Record<string, Record<string, string>>;
}

export interface IStoreCreationDataSource {
  createTemplate(params: { name: string; category: string; designParams: Record<string, any> }): Promise<WebsiteTemplate>;
  updatePageComponents(websiteId: string, pageId: string, components: PageComponent[]): Promise<PageConfig>;
  adaptResponsiveLayout(websiteId: string, pageId: string, deviceType: 'desktop' | 'tablet' | 'mobile'): Promise<PageComponent[]>;
  setupMultilingual(websiteId: string, languages: string[]): Promise<MultilingualConfig>;
  createBrand(params: { name: string; designAssets: Record<string, any> }): Promise<BrandConfig>;
  manageContent(brandId: string, contentData: { type: string; content: string }): Promise<{ success: boolean; message: string }>;
  integrateEcommerce(websiteId: string, features: string[]): Promise<{ success: boolean; config: Record<string, any> }>;
  integrateMarketing(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; config: Record<string, any> }>;
  configureShipping(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; config: Record<string, any> }>;
  generateAnalytics(websiteId: string, params: { metrics: string[]; timeRange: { start: string; end: string } }): Promise<Record<string, any>>;
  optimizeSEO(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; suggestions: string[] }>;
  integrateSocialMedia(websiteId: string, accounts: Array<{ platform: string; accountId: string }>): Promise<{ success: boolean; config: Record<string, any> }>;
  manageContentMarketing(websiteId: string, contentData: Record<string, any>): Promise<{ success: boolean; contentId: string }>;
  spreadBrandStory(brandId: string, params: { channels: string[]; schedule?: string }): Promise<{ success: boolean; reach: number }>;
}

class MockStoreCreationDataSource implements IStoreCreationDataSource {
  async createTemplate(params: { name: string; category: string; designParams: Record<string, any> }): Promise<WebsiteTemplate> {
    return {
      id: `tpl_${Date.now()}`,
      name: params.name,
      category: params.category as WebsiteTemplate['category'],
      layout: params.designParams.layout || {},
      styles: params.designParams.styles || {},
      components: params.designParams.components || [],
      status: 'draft',
    };
  }

  async updatePageComponents(websiteId: string, pageId: string, components: PageComponent[]): Promise<PageConfig> {
    return { id: pageId, websiteId, name: 'Page', path: '/page', components, seoConfig: {} };
  }

  async adaptResponsiveLayout(websiteId: string, pageId: string, deviceType: 'desktop' | 'tablet' | 'mobile'): Promise<PageComponent[]> {
    return [];
  }

  async setupMultilingual(websiteId: string, languages: string[]): Promise<MultilingualConfig> {
    return { id: `lang_${Date.now()}`, websiteId, defaultLanguage: languages[0] || 'en', supportedLanguages: languages, translations: {} };
  }

  async createBrand(params: { name: string; designAssets: Record<string, any> }): Promise<BrandConfig> {
    return {
      id: `brand_${Date.now()}`,
      name: params.name,
      logo: params.designAssets.logo,
      colors: params.designAssets.colors || {},
      fonts: params.designAssets.fonts || {},
      story: params.designAssets.story,
      values: params.designAssets.values || [],
    };
  }

  async manageContent(brandId: string, contentData: { type: string; content: string }): Promise<{ success: boolean; message: string }> {
    return { success: true, message: 'Content updated' };
  }

  async integrateEcommerce(websiteId: string, features: string[]): Promise<{ success: boolean; config: Record<string, any> }> {
    const config: Record<string, any> = {};
    for (const feature of features) {
      config[feature] = { enabled: true, settings: {} };
    }
    return { success: true, config };
  }

  async integrateMarketing(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; config: Record<string, any> }> {
    return { success: true, config: params };
  }

  async configureShipping(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; config: Record<string, any> }> {
    return { success: true, config: params };
  }

  async generateAnalytics(websiteId: string, params: { metrics: string[]; timeRange: { start: string; end: string } }): Promise<Record<string, any>> {
    const report: Record<string, any> = {};
    for (const metric of params.metrics) {
      report[metric] = { value: Math.floor(Math.random() * 10000), trend: 'up', change: '+5%' };
    }
    return report;
  }

  async optimizeSEO(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; suggestions: string[] }> {
    return { success: true, suggestions: ['Add meta description', 'Optimize images'] };
  }

  async integrateSocialMedia(websiteId: string, accounts: Array<{ platform: string; accountId: string }>): Promise<{ success: boolean; config: Record<string, any> }> {
    const config: Record<string, any> = {};
    for (const account of accounts) {
      config[account.platform] = { accountId: account.accountId, connected: true };
    }
    return { success: true, config };
  }

  async manageContentMarketing(websiteId: string, contentData: Record<string, any>): Promise<{ success: boolean; contentId: string }> {
    return { success: true, contentId: `content_${Date.now()}` };
  }

  async spreadBrandStory(brandId: string, params: { channels: string[]; schedule?: string }): Promise<{ success: boolean; reach: number }> {
    return { success: true, reach: Math.floor(Math.random() * 100000) + 1000 };
  }
}

class ApiStoreCreationDataSource implements IStoreCreationDataSource {
  private baseUrl = '/api/omnichannel';

  async createTemplate(params: { name: string; category: string; designParams: Record<string, any> }): Promise<WebsiteTemplate> {
    const res = await http.post(`${this.baseUrl}/store/template`, params);
    return res.data;
  }

  async updatePageComponents(websiteId: string, pageId: string, components: PageComponent[]): Promise<PageConfig> {
    const res = await http.put(`${this.baseUrl}/store/website/${websiteId}/page/${pageId}/components`, { components });
    return res.data;
  }

  async adaptResponsiveLayout(websiteId: string, pageId: string, deviceType: 'desktop' | 'tablet' | 'mobile'): Promise<PageComponent[]> {
    const res = await http.get(`${this.baseUrl}/store/website/${websiteId}/page/${pageId}/adapt?deviceType=${deviceType}`);
    return res.data;
  }

  async setupMultilingual(websiteId: string, languages: string[]): Promise<MultilingualConfig> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/multilingual`, { languages });
    return res.data;
  }

  async createBrand(params: { name: string; designAssets: Record<string, any> }): Promise<BrandConfig> {
    const res = await http.post(`${this.baseUrl}/store/brand`, params);
    return res.data;
  }

  async manageContent(brandId: string, contentData: { type: string; content: string }): Promise<{ success: boolean; message: string }> {
    const res = await http.post(`${this.baseUrl}/store/brand/${brandId}/content`, contentData);
    return res.data;
  }

  async integrateEcommerce(websiteId: string, features: string[]): Promise<{ success: boolean; config: Record<string, any> }> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/ecommerce`, { features });
    return res.data;
  }

  async integrateMarketing(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; config: Record<string, any> }> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/marketing`, { params });
    return res.data;
  }

  async configureShipping(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; config: Record<string, any> }> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/shipping`, { params });
    return res.data;
  }

  async generateAnalytics(websiteId: string, params: { metrics: string[]; timeRange: { start: string; end: string } }): Promise<Record<string, any>> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/analytics`, params);
    return res.data;
  }

  async optimizeSEO(websiteId: string, params: Record<string, any>): Promise<{ success: boolean; suggestions: string[] }> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/seo`, { params });
    return res.data;
  }

  async integrateSocialMedia(websiteId: string, accounts: Array<{ platform: string; accountId: string }>): Promise<{ success: boolean; config: Record<string, any> }> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/social`, { accounts });
    return res.data;
  }

  async manageContentMarketing(websiteId: string, contentData: Record<string, any>): Promise<{ success: boolean; contentId: string }> {
    const res = await http.post(`${this.baseUrl}/store/website/${websiteId}/content-marketing`, { contentData });
    return res.data;
  }

  async spreadBrandStory(brandId: string, params: { channels: string[]; schedule?: string }): Promise<{ success: boolean; reach: number }> {
    const res = await http.post(`${this.baseUrl}/store/brand/${brandId}/spread`, params);
    return res.data;
  }
}

const useMock = process.env.NODE_ENV === 'development' || process.env.REACT_APP_USE_MOCK === 'true';
export const storeCreationDataSource: IStoreCreationDataSource = useMock
  ? new MockStoreCreationDataSource()
  : new ApiStoreCreationDataSource();
