from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from django.views.generic import RedirectView
from .views import (
    ActionsPageView,
    AddSatellitesView,
    AddTranspondersView,
    # ClusterTestView,
    ClearLyngsatCacheView,
    DataEntryView,
    DeleteSelectedObjectsView,
    DeleteSelectedSourcesView,
    DeleteSelectedTranspondersView,
    DeleteSelectedSatellitesView,
    FillLyngsatDataView,
    GeoPointsAPIView,
    GetLocationsView,
    HomeView,
    KubsatView,
    KubsatExportView,
    KubsatCreateRequestsView,
    KubsatRecalculateCoordsView,
    LinkLyngsatSourcesView,
    LinkVchSigmaView,
    LoadCsvDataView,
    LoadExcelDataView,
    LyngsatDataAPIView,
    LyngsatTaskStatusAPIView,
    LyngsatTaskStatusView,
    MergeSourcesView,
    MultiSourcesPlaybackDataAPIView,
    MultiSourcesPlaybackMapView,
    ObjItemCreateView,
    ObjItemDeleteView,
    ObjItemDetailView,
    ObjItemListView,
    ObjItemUpdateView,
    ProcessKubsatView,
    SatelliteDataAPIView,
    SatelliteTranspondersAPIView,
    SatelliteListView,
    SatelliteCreateView,
    SatelliteUpdateView,
    SearchObjItemAPIView,
    ShowMapView,
    ShowSelectedObjectsMapView,
    ShowSourcesMapView,
    ShowSourceWithPointsMapView,
    ShowSourceAveragingStepsMapView,
    SourceListView,
    SourceCreateView,
    SourceUpdateView,
    SourceDeleteView,
    SourceObjItemsAPIView,
    SigmaParameterDataAPIView,
    TransponderDataAPIView,
    TransponderListView,
    TransponderCreateView,
    TransponderUpdateView,
    UnlinkAllLyngsatSourcesView,
    UploadVchLoadView,
    custom_logout,
)
from .views.marks import (
    SignalMarksView,
    SignalMarksHistoryAPIView,
    SignalMarksEntryAPIView,
    SaveSignalMarksView,
    CreateTechAnalyzeView,
    ObjectMarksListView,
    AddObjectMarkView,
    UpdateObjectMarkView,
)
from .views.source_requests import (
    SourceRequestListView,
    SourceRequestCreateView,
    SourceRequestUpdateView,
    SourceRequestDeleteView,
    SourceRequestBulkDeleteView,
    SourceRequestExportView,
    SourceRequestAPIView,
    SourceRequestDetailAPIView,
    SourceDataAPIView,
    SourceRequestImportView,
)
from .views.tech_analyze import (
    TechAnalyzeEntryView,
    TechAnalyzeSaveView,
    LinkExistingPointsView,
    TechAnalyzeListView,
    TechAnalyzeDeleteView,
    TechAnalyzeAPIView,
)
from .views.points_averaging import PointsAveragingView, PointsAveragingAPIView, RecalculateGroupAPIView
from .views.statistics import StatisticsView, StatisticsAPIView, ExtendedStatisticsAPIView
from .views.secret_stats import SecretStatsView
from .views.user_permissions import (
    UserPermissionsListView,
    UserPermissionsEditView,
    UserPermissionsApiView,
    InitPermissionsView,
)
from .views.errors_report import (
    ErrorsReportView,
    ErrorsReportAPIView,
    ErrorsReportSaveAPIView,
    ErrorsReportDeleteAPIView,
    WeeklyHoursAPIView,
)

app_name = 'mainapp'

urlpatterns = [
    # Root URL now points to SourceListView (Requirement 1.1)
    path('', SourceListView.as_view(), name='home'),
    # Redirect old /home/ URL to source_list for backward compatibility (Requirement 1.2)
    path('home/', RedirectView.as_view(pattern_name='mainapp:source_list', permanent=True), name='home_redirect'),
    # Keep /sources/ as an alias (Requirement 1.2)
    path('sources/', SourceListView.as_view(), name='source_list'),
    path('source/create/', SourceCreateView.as_view(), name='source_create'),
    path('source/<int:pk>/edit/', SourceUpdateView.as_view(), name='source_update'),
    path('source/<int:pk>/delete/', SourceDeleteView.as_view(), name='source_delete'),
    path('delete-selected-sources/', DeleteSelectedSourcesView.as_view(), name='delete_selected_sources'),
    path('merge-sources/', MergeSourcesView.as_view(), name='merge_sources'),
    path('objitems/', ObjItemListView.as_view(), name='objitem_list'),
    path('transponders/', TransponderListView.as_view(), name='transponder_list'),
    path('transponder/create/', TransponderCreateView.as_view(), name='transponder_create'),
    path('transponder/<int:pk>/edit/', TransponderUpdateView.as_view(), name='transponder_update'),
    path('delete-selected-transponders/', DeleteSelectedTranspondersView.as_view(), name='delete_selected_transponders'),
    path('satellites/', SatelliteListView.as_view(), name='satellite_list'),
    path('satellite/create/', SatelliteCreateView.as_view(), name='satellite_create'),
    path('satellite/<int:pk>/edit/', SatelliteUpdateView.as_view(), name='satellite_update'),
    path('delete-selected-satellites/', DeleteSelectedSatellitesView.as_view(), name='delete_selected_satellites'),
    path('actions/', ActionsPageView.as_view(), name='actions'),
    path('excel-data', LoadExcelDataView.as_view(), name='load_excel_data'),
    path('satellites', AddSatellitesView.as_view(), name='add_sats'),
    path('api/locations/<int:sat_id>/geojson/', GetLocationsView.as_view(), name='locations_by_id'),
    path('transponders', AddTranspondersView.as_view(), name='add_trans'),
    path('csv-data', LoadCsvDataView.as_view(), name='load_csv_data'),
    path('map-points/', ShowMapView.as_view(), name='admin_show_map'),
    path('show-selected-objects-map/', ShowSelectedObjectsMapView.as_view(), name='show_selected_objects_map'),
    path('show-sources-map/', ShowSourcesMapView.as_view(), name='show_sources_map'),
    path('show-source-with-points-map/<int:source_id>/', ShowSourceWithPointsMapView.as_view(), name='show_source_with_points_map'),
    path('show-source-averaging-map/<int:source_id>/', ShowSourceAveragingStepsMapView.as_view(), name='show_source_averaging_map'),
    path('multi-sources-playback-map/', MultiSourcesPlaybackMapView.as_view(), name='multi_sources_playback_map'),
    path('delete-selected-objects/', DeleteSelectedObjectsView.as_view(), name='delete_selected_objects'),
    # path('cluster/', ClusterTestView.as_view(), name='cluster'),
    path('vch-upload/', UploadVchLoadView.as_view(), name='vch_load'),
    path('vch-link/', LinkVchSigmaView.as_view(), name='link_vch_sigma'),
    path('link-lyngsat/', LinkLyngsatSourcesView.as_view(), name='link_lyngsat'),
    path('api/lyngsat/<int:lyngsat_id>/', LyngsatDataAPIView.as_view(), name='lyngsat_data_api'),
    path('api/sigma-parameter/<int:parameter_id>/', SigmaParameterDataAPIView.as_view(), name='sigma_parameter_data_api'),
    path('api/source/<int:source_id>/objitems/', SourceObjItemsAPIView.as_view(), name='source_objitems_api'),
    path('api/transponder/<int:transponder_id>/', TransponderDataAPIView.as_view(), name='transponder_data_api'),
    path('api/satellite/<int:satellite_id>/', SatelliteDataAPIView.as_view(), name='satellite_data_api'),
    path('api/satellite/<int:satellite_id>/transponders/', SatelliteTranspondersAPIView.as_view(), name='satellite_transponders_api'),
    path('api/geo-points/', GeoPointsAPIView.as_view(), name='geo_points_api'),
    path('api/multi-sources-playback/', MultiSourcesPlaybackDataAPIView.as_view(), name='multi_sources_playback_api'),
    path('kubsat-excel/', ProcessKubsatView.as_view(), name='kubsat_excel'),
    path('object/create/', ObjItemCreateView.as_view(), name='objitem_create'),
    path('object/<int:pk>/edit/', ObjItemUpdateView.as_view(), name='objitem_update'),
    path('object/<int:pk>/', ObjItemDetailView.as_view(), name='objitem_detail'),
    path('object/<int:pk>/delete/', ObjItemDeleteView.as_view(), name='objitem_delete'),
    path('fill-lyngsat-data/', FillLyngsatDataView.as_view(), name='fill_lyngsat_data'),
    path('lyngsat-task-status/', LyngsatTaskStatusView.as_view(), name='lyngsat_task_status'),
    path('lyngsat-task-status/<str:task_id>/', LyngsatTaskStatusView.as_view(), name='lyngsat_task_status'),
    path('api/lyngsat-task-status/<str:task_id>/', LyngsatTaskStatusAPIView.as_view(), name='lyngsat_task_status_api'),
    path('clear-lyngsat-cache/', ClearLyngsatCacheView.as_view(), name='clear_lyngsat_cache'),
    path('unlink-all-lyngsat/', UnlinkAllLyngsatSourcesView.as_view(), name='unlink_all_lyngsat'),
    # Signal Marks (новая система отметок)
    path('signal-marks/', SignalMarksView.as_view(), name='signal_marks'),
    path('api/signal-marks/history/', SignalMarksHistoryAPIView.as_view(), name='signal_marks_history_api'),
    path('api/signal-marks/entry/', SignalMarksEntryAPIView.as_view(), name='signal_marks_entry_api'),
    path('api/signal-marks/save/', SaveSignalMarksView.as_view(), name='save_signal_marks'),
    path('api/signal-marks/create-tech-analyze/', CreateTechAnalyzeView.as_view(), name='create_tech_analyze_for_marks'),
    # Старые URL для обратной совместимости (редирект)
    path('object-marks/', ObjectMarksListView.as_view(), name='object_marks'),
    path('api/add-object-mark/', AddObjectMarkView.as_view(), name='add_object_mark'),
    path('api/update-object-mark/', UpdateObjectMarkView.as_view(), name='update_object_mark'),
    path('kubsat/', KubsatView.as_view(), name='kubsat'),
    path('kubsat/export/', KubsatExportView.as_view(), name='kubsat_export'),
    path('kubsat/create-requests/', KubsatCreateRequestsView.as_view(), name='kubsat_create_requests'),
    path('kubsat/recalculate-coords/', KubsatRecalculateCoordsView.as_view(), name='kubsat_recalculate_coords'),
    # Source Requests
    path('source-requests/', SourceRequestListView.as_view(), name='source_request_list'),
    path('source-requests/create/', SourceRequestCreateView.as_view(), name='source_request_create'),
    path('source-requests/<int:pk>/edit/', SourceRequestUpdateView.as_view(), name='source_request_update'),
    path('source-requests/<int:pk>/delete/', SourceRequestDeleteView.as_view(), name='source_request_delete'),
    path('api/source/<int:source_id>/requests/', SourceRequestAPIView.as_view(), name='source_requests_api'),
    path('api/source-request/<int:pk>/', SourceRequestDetailAPIView.as_view(), name='source_request_detail_api'),
    path('api/source/<int:source_id>/data/', SourceDataAPIView.as_view(), name='source_data_api'),
    path('source-requests/import/', SourceRequestImportView.as_view(), name='source_request_import'),
    path('source-requests/export/', SourceRequestExportView.as_view(), name='source_request_export'),
    path('source-requests/bulk-delete/', SourceRequestBulkDeleteView.as_view(), name='source_request_bulk_delete'),
    path('data-entry/', DataEntryView.as_view(), name='data_entry'),
    path('api/search-objitem/', SearchObjItemAPIView.as_view(), name='search_objitem_api'),
    path('tech-analyze/', TechAnalyzeEntryView.as_view(), name='tech_analyze_entry'),
    path('tech-analyze/list/', TechAnalyzeListView.as_view(), name='tech_analyze_list'),
    path('tech-analyze/save/', TechAnalyzeSaveView.as_view(), name='tech_analyze_save'),
    path('tech-analyze/delete/', TechAnalyzeDeleteView.as_view(), name='tech_analyze_delete'),
    path('tech-analyze/link-existing/', LinkExistingPointsView.as_view(), name='tech_analyze_link_existing'),
    path('api/tech-analyze/', TechAnalyzeAPIView.as_view(), name='tech_analyze_api'),
    path('points-averaging/', PointsAveragingView.as_view(), name='points_averaging'),
    path('api/points-averaging/', PointsAveragingAPIView.as_view(), name='points_averaging_api'),
    path('api/points-averaging/recalculate/', RecalculateGroupAPIView.as_view(), name='points_averaging_recalculate'),
    path('statistics/', StatisticsView.as_view(), name='statistics'),
    path('api/statistics/', StatisticsAPIView.as_view(), name='statistics_api'),
    path('api/statistics/extended/', ExtendedStatisticsAPIView.as_view(), name='extended_statistics_api'),
    path('secret-stat/', SecretStatsView.as_view(), name='secret_stats'),
    path('logout/', custom_logout, name='logout'),
    
    # User permissions management
    path('user-permissions/', UserPermissionsListView.as_view(), name='user_permissions_list'),
    path('user-permissions/<int:pk>/edit/', UserPermissionsEditView.as_view(), name='user_permissions_edit'),
    path('user-permissions/init/', InitPermissionsView.as_view(), name='init_permissions'),
    path('api/user-permissions/<int:pk>/', UserPermissionsApiView.as_view(), name='user_permissions_api'),
    
    # Errors Report (Журнал ошибок)
    path('errors-report/', ErrorsReportView.as_view(), name='errors_report'),
    path('api/errors-report/', ErrorsReportAPIView.as_view(), name='errors_report_api'),
    path('api/errors-report/save/', ErrorsReportSaveAPIView.as_view(), name='errors_report_save'),
    path('api/errors-report/delete/', ErrorsReportDeleteAPIView.as_view(), name='errors_report_delete'),
    path('api/errors-report/weekly-hours/', WeeklyHoursAPIView.as_view(), name='weekly_hours_api'),
]